//
//  UIColorEx.swift
//  SFBase_Example
//
//  Created by fengfeng on 2025/2/18.
//  Copyright © 2025 CocoaPods. All rights reserved.
//

import UIKit

public extension UIColor {
    /// Create a UIColor with different colors for light and dark mode.
    ///
    /// - Parameters:
    ///     - light: Color to use in light/unspecified mode.
    ///     - dark: Color to use in dark mode.
    convenience init(light: UIColor, dark: UIColor) {
        if #available(iOS 13.0, *) {
            self.init(dynamicProvider: { $0.userInterfaceStyle == .dark ? dark : light })
        } else {
            self.init(cgColor: light.cgColor)
        }
    }
    
    convenience init(lightr: CGFloat, ligntg: CGFloat, ligntb: CGFloat, lignta: CGFloat = 1.0, darkr: CGFloat, darkg: CGFloat, darkb: CGFloat, darka: CGFloat = 1.0) {
        let l = UIColor.init(r: lightr, g: ligntg, b: ligntb,a: lignta)
        let d = UIColor.init(r: darkr, g: darkg, b: darkb,a: darka)
        if #available(iOS 13.0, *) {
            self.init(dynamicProvider: { $0.userInterfaceStyle == .dark ? d : l })
        } else {
            self.init(cgColor: l.cgColor)
        }
    }
    
    convenience init(light: Int, dark: Int) {
        let l = UIColor(hexValue: light)
        let d = UIColor(hexValue: dark)
        if #available(iOS 13.0, *) {
            self.init(dynamicProvider: { $0.userInterfaceStyle == .dark ? d : l })
        } else {
            self.init(cgColor: l.cgColor)
        }
    }
    
    convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1.0) {
        self.init(red: r/255.0, green: g/255.0, blue:b/255.0, alpha:a)
    }
    
    convenience init(hexValue: Int, alpha: CGFloat = 1.0) {
        self.init(red: CGFloat((hexValue & 0xFF0000) >> 16) / 255.0,
                  green: CGFloat((hexValue & 0x00FF00) >> 8) / 255.0,
                  blue: CGFloat(hexValue & 0x0000FF) / 255.0,
                  alpha: CGFloat(alpha))
    }
    
    class var randomColor: UIColor {
        let red = CGFloat(Int.random(in: 0...255))
        let green = CGFloat(Int.random(in: 0...255))
        let blue = CGFloat(Int.random(in: 0...255))
        return UIColor.init(r: red, g: green, b: blue)
    }
}
